package com.CampaignManager.shared;

import java.util.Date;

public class CalendarUtil {
	
	@SuppressWarnings("deprecation") // GWT requires Date
	  private static void resetTime(Date date) {
	    long msec = date.getTime();
	    msec = (msec / 1000) * 1000;
	    date.setTime(msec);

	    // Daylight savings time occurs at midnight in some time zones, so we reset
	    // the time to noon instead.
	    date.setHours(12);
	    date.setMinutes(0);
	    date.setSeconds(0);
	}
	
	public static Date copyDate(Date date) {
	    if (date == null) {
	      return null;
	    }
	    Date newDate = new Date();
	    newDate.setTime(date.getTime());
	    return newDate;
    }
	
	public static int getDaysBetween(Date start, Date finish) {
	    // Convert the dates to the same time
	    start = copyDate(start);
	    resetTime(start);
	    finish = copyDate(finish);
	    resetTime(finish);

	    long aTime = start.getTime();
	    long bTime = finish.getTime();

	    long adjust = 60 * 60 * 1000;
	    adjust = (bTime > aTime) ? adjust : -adjust;

	    return (int) ((bTime - aTime + adjust) / (24 * 60 * 60 * 1000));
	}
}
